﻿using AutoMapper;
using B.Rabbit.DataBase.Domain;
using B.Rabbit.DataBase.ErrorCode;
using B.Rabbit.DataBase.Infrastructure.Infrastructure;
using B.Rabbit.DataBase.Read.Api.Applocation.Command.Rabbit.Health;
using B.Rabbit.DataBase.Read.Api.DTOs;
using MediatR;

namespace B.Rabbit.DataBase.Read.Api.Applocation.Handler.Rabbit.Health
{
    public class GetHealthCommandHandler : IRequestHandler<GetHealthCommand, ApiResult<List<GetHealthDTOs>>>
    {
        IBaseRepository<HealthCareModel> healthmodel;
        IMapper mapper;

        public GetHealthCommandHandler(IBaseRepository<HealthCareModel> healthmodel, IMapper mapper)
        {
            this.healthmodel = healthmodel;
            this.mapper = mapper;
        }
        /// <summary>
        /// 保健信息查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<GetHealthDTOs>>> Handle(GetHealthCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<GetHealthDTOs>> res = new ApiResult<List<GetHealthDTOs>>();
            var health = healthmodel.GetList().Where(x => x.IsDel == false);
            if (!string.IsNullOrEmpty(request.Bacth))
            {
                health = health.Where(x => x.ProductionBatch == request.Bacth);
            }

            var entity = mapper.Map<List<GetHealthDTOs>>(health);

            res.data = entity;
            res.msg = "成功获取";
            res.code = StatusCode.Ok;
            return Task.FromResult(res);
        }
    }
}
